package quanlyhochieu.func01;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;

import javax.faces.model.SelectItem;

import quanlyhochieu.QLHCF1234TableItemInfo;

import dbutil.ConnectDB;
import exportExcel.ExportExcel;
import utils.UtilsOnScopes;

/******************************************
 * Filename			: QLHCF01FuncInfo.java
 * Description		: Bean cho chuc nang 01 cua QLHC
 * Created date		: Aug 16, 2008
 * Author			: Phonglt
 ******************************************/
public class QLHCF01FuncInfo {
	private String error;
	private int typeQuery;
	private ArrayList<QLHCF1234TableItemInfo> items = new ArrayList<QLHCF1234TableItemInfo>();
	private ArrayList<HashMap<String, Object>> database = new ArrayList<HashMap<String,Object>>();
	private String linkDownload;
	
	/****************************************************
	 * Method name		: getTypeQuery [GETTER]
	 * Return type		: QLHCF01FuncInfo
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public int getTypeQuery() {
		return typeQuery;
	}
	/****************************************************
	 * Method name		: setTypeQuery [SETTER]
	 * Return type		: void
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setTypeQuery(int typeQuery) {
		this.typeQuery = typeQuery;
	}
	/****************************************************
	 * Method name		: getError [GETTER]
	 * Return type		: QLHCF01FuncInfo
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public String getError() {
		return error;
	}
	/****************************************************
	 * Method name		: setError [SETTER]
	 * Return type		: void
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setError(String error) {
		this.error = error;
	}

	/****************************************************
	 * Method name		: getItems [GETTER]
	 * Return type		: QLHCF01FuncInfo
	 * Description		: get data for view in UI
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public ArrayList<QLHCF1234TableItemInfo> getItems() {
		this.setItems(new ArrayList<QLHCF1234TableItemInfo>());
		String SQL = "SELECT A.sohc, A.loaihc, A.ngaycap, A.ngayhethan, A.noicap, canhan.* " +
				" FROM hochieu A, canhan WHERE A.cmnd = canhan.cmnd";
		switch (this.typeQuery) {
		case 2:
			SQL += " AND loaihc = 'Ngoại Giao' ";
			break;
		case 3:
			SQL += " AND loaihc = 'Công Vụ' ";
			break;
		case 4:
			SQL += " AND loaihc = 'Phổ Thông' ";
			break;
		default:
			break;
		}
		SQL += " ORDER BY ngaycap";
		System.out.println(SQL);
		
		try {
			ArrayList<HashMap<String, Object>> arr = ConnectDB
					.getDataTypeList(SQL);
			System.out.println("size "+arr.size());
			if (arr.size() > 0) {
				ArrayList<QLHCF1234TableItemInfo> temp_items = new ArrayList<QLHCF1234TableItemInfo>(
						arr.size());
				
				for (int i = 1; i <= arr.size(); i++) {
					HashMap<String, Object> row = arr.get(i - 1);
					QLHCF1234TableItemInfo item = new QLHCF1234TableItemInfo(i,
							(String) row.get("sohc"), (String) row
									.get("loaihc"), (Date) row.get("ngaycap"),
							(Date) row.get("ngayhethan"), (String) row
									.get("noicap"), (String) row.get("cmnd"),
							(String) row.get("ten"),
							(String) row.get("namsinh"), ((Boolean) row
									.get("gioitinh")) == true ? "Nam" : "Nữ",
							(String) row.get("coquan"), ((Boolean) row
									.get("dangvien")) == true ? "Đảng viên" : "-",
							(String) row.get("chucvu"));
					temp_items.add(item);
				}
				this.setItems(temp_items);
				this.database = arr;
			}
			}catch (Exception e) {
				e.printStackTrace();
			}
				
		return items;
	}
	/****************************************************
	 * Method name		: setItems [SETTER]
	 * Return type		: void
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setItems(ArrayList<QLHCF1234TableItemInfo> items) {
		this.items = items;
	}
	
	/****************************************************
	 * Method name		: getLinkDownload [GETTER]
	 * Return type		: QLHCF01FuncInfo
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public String getLinkDownload() {
		return linkDownload;
	}
	/****************************************************
	 * Method name		: setLinkDownload [SETTER]
	 * Return type		: void
	 * Description		: 
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 ****************************************************/
	public void setLinkDownload(String linkDownload) {
		this.linkDownload = linkDownload;
	}
	/**************************************
	 * Method name		: getLabelSearch
	 * Return type		: SelectItem[]
	 * Description		:
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 **************************************/
	public SelectItem[] getLabelSearch(){
		SelectItem[] labels = new SelectItem[4];
		labels[0] = new SelectItem(1, "Tất cả");
		labels[1] = new SelectItem(2, "Ngoại Giao");
		labels[2] = new SelectItem(3, "Công Vụ");
		labels[3] = new SelectItem(4, "Phổ Thông");
		return labels;
	}
	
	/**************************************
	 * Method name		: getTongSo
	 * Return type		: int
	 * Description		:
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 **************************************/
	public int getTongSo(){
		return this.items.size();
	}
	
	/**************************************
	 * Method name		: exportExel
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 16, 2008
	 * Author			: Phonglt
	 **************************************/
	public String exportExel(){
		if(this.database != null && this.database.size() > 0){
			Hashtable<String, String> header = new Hashtable<String, String>();
			header.put("TONG",String.valueOf(this.getTongSo()));
		ExportExcel.exportJExcel_HaveGroup("quanlyhochieu_template",
				"hochieuhienco.xml", "exp_quanlyhochieu", this.database,
				UtilsOnScopes.getRequest(), header, null);
		this.setLinkDownload(ExportExcel.pathDownload);
		}else{
			this.setError("Không có dữ liệu được báo cáo.");
			return "qlhcfunc01";
		}
		return "download";
	}
	
	public String comeback(){
		return "qlhcfunc01";
	}
	
}
